package com.android.friend.app;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

public class MyAndroidLibApp extends Application {
    private static Context mContext;
    public static int CACHE_LIMIT_FOR_IMAGE;
    public static Context APP_CONTEXT;
    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        int cacheLimit =
                1024 * 1024 * (((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE))
                        .getMemoryClass() / 4);
        CACHE_LIMIT_FOR_IMAGE = cacheLimit;
        APP_CONTEXT = getApplicationContext();
        initImageLoader(getApplicationContext());
    }

    public static Context getAppContext() {
        return mContext;
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
//        if (SharedData.getInstance().getImageLoader() != null) {
//            SharedData.getInstance().getImageLoader().clearCache();
//        }
    }

    public static final boolean DEVELOPER_MODE = false;

    @SuppressWarnings("unused")
    public static void initImageLoader(Context context) {
        // This configuration tuning is custom. You can tune every option, you
        // may tune some of
        // them,
        // or you can create default configuration by
        // ImageLoaderConfiguration.createDefault(this);
        // method.
        DisplayImageOptions options =
                new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true)
                        .imageScaleType(ImageScaleType.IN_SAMPLE_INT).bitmapConfig(Bitmap.Config.RGB_565)
                        .showImageOnFail(R.drawable.book_default).build();

        ImageLoaderConfiguration config =
                new ImageLoaderConfiguration.Builder(context).threadPriority(Thread.NORM_PRIORITY - 2)
                        .denyCacheImageMultipleSizesInMemory().threadPoolSize(3)
                        .discCacheFileNameGenerator(new Md5FileNameGenerator())
                        .defaultDisplayImageOptions(options) // in common
                        .memoryCache(new WeakMemoryCache()).build();
        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config);
    }

}
